//@author A0101119Y
#include "StorageMgr.h"

using namespace Twentyfifth;
using namespace std;

StorageMgr::StorageMgr() {
	try {
		cout << "Loading..." << endl;
		gcalmgr.setDbMgr(database);
		gcalmgr.syncDatabase(database);
	}
	catch (Poco::Exception &e) {
		cerr << e.code() << " " << e.className() << ": " << e.displayText() << endl;
	}
	catch (exception&) {
	}
	//Cmd c;
	//lastCmd = c;
}

StorageMgr::~StorageMgr() {
	try {
		cout << "Exiting..." << endl;
		gcalmgr.syncDatabase(database);
	}
	catch (Poco::Exception &e) {
		cerr << e.code() << " " << e.className() << ": " << e.displayText() << endl;
	}
	catch (exception&) {
	}
}

StorageMgr::StorageMgr(string filename) {
	database.DbMgr::DbMgr(filename);
	gcalmgr.setDbMgr(database);
}

Status StorageMgr::viewTask(int id) {
	Status s;
	s = database.viewTask(id);
	return s;
}

Status StorageMgr::viewTasks() {
	Status s;
	s = database.viewTasks();
	return s;
}

Status StorageMgr::addTask(Task t) {
	Status s;
	s = database.addTask(t);
	return s;
}

Status StorageMgr::editTask(int id, TaskParams param, DateTime dt) {
	Status s = database.editTask(id, param, dt);
	return s;
}

Status StorageMgr::editTask(int id, TaskParams param, string str) {
	Status s = database.editTask(id, param, str);
	return s;
}

Status StorageMgr::editTask(int id, TaskParams param, bool b) {
	Status s = database.editTask(id, param, b);
	return s;
}

Status StorageMgr::deleteTask(int id, bool isPermanent) {
	Status s = database.deleteTask(id, isPermanent);
	return s;
}

Status StorageMgr::undeleteTask(int id) {
	Status s = database.undeleteTask(id);
	return s;
}

Status StorageMgr::clearTasks() {
	Status s = database.clearTasks();
	return s;
}

Status StorageMgr::searchTasks(string searchstring) {
	Status s = database.searchTasks(searchstring);
	return s;
}

Status StorageMgr::searchTasks(DateTime searchdt) {
	Status s = database.searchTasks(searchdt);
	return s;
}

Status StorageMgr::executeGCal(string userInput) {
	Status s = gcalmgr.executeCommand(userInput);
	if (!s.tasks.empty()) {
		gcalmgr.syncDatabase(database);
	}
	return s;
}